<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Cms_menu extends AppModel {
    var $_primary_key = CMS_MENU_ID;
    var $_tablename = CMS_MENUS_TABLE;
    var $_mapping = array(
                           array('id' => CMS_MENU_ID),
                           array('name' => CMS_MENU_NAME),
                           array('parent_id' => CMS_MENU_PARENT_ID),
                           array('link' => CMS_MENU_LINK),
                           array('ordering' => CMS_MENU_ORDERING),
                           array('enabled' => CMS_MENU_ENABLED),
                           array('param' => CMS_MENU_PARAM),
                            );
    var $_cache = 'menus_cache';
    public function Cms_menu($id = NULL){
        parent::AppModel($id);
    }
    
    public function menu_list_tree(){
        $menus = $this->menus_recursive();
        $result = array();
        foreach($menus as $m){
            $result[] = $m;
            $this->_build_tree($m->children, $m->name, $result);
        }
        return $result;
    }
    
    public function menus_recursive(){
        $CI = &get_instance();

        $menus = $this->menus_top();
        foreach($menus as &$menu){
            $menu->children = array();
            $this->menus_children($menu->id, $menu->children);
        }
        return $menus;
    }
    
    public function menus_top(){
        $condition = '('.CMS_MENU_PARENT_ID.' = 0 OR '.CMS_MENU_PARENT_ID.' IS NULL OR '.CMS_MENU_PARENT_ID.' = '.CMS_MENU_ID.')';
        $menus = $this->get_lists($condition, NULL, NULL, 'name', 0);
        return $menus;
    }
    
    public function menus_children($parent_id = 0, &$result){
        $condition = CMS_MENU_PARENT_ID.' = '.$parent_id;

        $menus = $this->get_lists($condition , NULL, NULL, 'name', 0);
        if(count($menus)==0)
            return;
        foreach($menus as $menu){
            $menu->children = array();
            $this->menus_children($menu->id, $menu->children);
            $result[] = $menu;
        }
    }
    
    private function _build_tree($children, $name, &$result){
        foreach($children as &$child){
            $child->name = $name.' > '.$child->name;
            $result[] = $child;
            if($child->children)
                $this->_build_tree($child->children, $child->name, $result);
        }
    }
    // Get parent menus of current menu item
    public function parent_recursive(){
        if(empty($this->parent_id))
            return array();
        $this->_parent_recursive($this->parent_id, $result);
        return array_reverse($result);
    }
    
    private function _parent_recursive($menu_id, &$result){
        $CI = &get_instance();
        $m = $CI->load->module_model_factory('menus.cms_menu', $menu_id);
        $result[] = $m;
        if(empty($m->parent_id))
            return;
        else{
            $this->_parent_recursive($m->parent_id, $result);
        }
    }
}
?>